import requests
from bs4 import BeautifulSoup

class DataProcessor:
    def __init__(self, url, request_method='POST'):
        self.url = url
        self.request_method = request_method.upper()

    def fetch_data(self, page_index=1, params=None):
        if self.request_method == 'POST':
            data = {"pageIndex": page_index}  # 添加 pageIndex 参数
            response = requests.post(self.url, data=data)
        elif self.request_method == 'GET':
            response = requests.get(self.url, params=params)
        else:
            raise ValueError("Invalid request method. Only 'GET' and 'POST' are supported.")
        
        response.encoding = 'utf-8'
        return response

    def parse_data(self, response, data_path, is_html=False):
        if response.status_code == 200:
            if is_html:
                data = BeautifulSoup(response.text, 'html.parser')
            else:
                data = response.json()

            parsed_data = data
            if not is_html:
                for key in data_path:
                    parsed_data = parsed_data[key]

            return parsed_data
        else:
            print("请求失败，状态码：", response.status_code)
            return None